package com.lottery.shadowbd.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.adapter.LotHistoryAdapter;
import com.lottery.shadowbd.bean.LotteryHistory;
import com.lottery.shadowbd.callback.ECallback;
import com.lottery.shadowbd.io.api.LotApiIO;
import com.lottery.shadowbd.support.SpacesItemDecoration;
import com.lottery.shadowbd.tuple.Tuple2;
import com.lottery.shadowbd.utils.LogUtils;
import com.lottery.shadowbd.utils.LotteryUtils;
import com.lottery.shadowbd.utils.ToastUtil;
import com.lottery.shadowbd.widget.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LotteryDrawActivity extends BaseActivity implements PullLoadMoreRecyclerView.PullLoadMoreListener {

    private ImageView iv_lot_back;
    private TextView tv_lot_title;

    private PullLoadMoreRecyclerView pull_load_lot;

    private LotHistoryAdapter mLotAdapter;
    private List<LotteryHistory.ListEntity> mList;

    private int page;

    private String lotId;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_lottery_draw);

        iv_lot_back = (ImageView) findViewById(R.id.iv_lot_back);

        tv_lot_title = (TextView) findViewById(R.id.tv_lot_title);

        pull_load_lot = (PullLoadMoreRecyclerView) findViewById(R.id.pull_load_lot);
    }

    @Override
    protected void initData() {
        mContext = this;

        if (getIntent() != null) {
            String name = getIntent().getStringExtra("name");

            tv_lot_title.setText(getIntent().getStringExtra("name"));

            LogUtils.e("name===" + name);

            Map<String, String> nameId = LotteryUtils.getAllAvailable();

            if (name != null) {
                lotId = nameId.get(name);
                LogUtils.e("id===" + lotId);
            }
        }

        mList = new ArrayList<>();

        mLotAdapter = new LotHistoryAdapter(mContext, mList);

        mLotAdapter.setId(lotId);

        pull_load_lot.setLinearLayout();
        pull_load_lot.addItemDecoration(new SpacesItemDecoration(2));
        pull_load_lot.setAdapter(mLotAdapter);

        getHistory();
    }

    @Override
    protected void initListener() {
        iv_lot_back.setOnClickListener(this);
        pull_load_lot.setmPullLoadMoreListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_lot_back://返回
                finish();
                break;
            default:
                break;
        }
    }

    private void getHistory() {
        if (lotId == null) {
            return;
        }

        if(!pull_load_lot.isRefresh()) {
            showLoadingDialog();
        }

        page = 1;

        LotApiIO.getInstance().getLotHistory(lotId, page, new ECallback<LotteryHistory, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(LotteryHistory res) {
                dismissDialog();
                if(pull_load_lot.isRefresh()) {
                    pull_load_lot.setRefreshing(false);
                }

                if(mList == null) {
                    return;
                }

                page++;

                mList.clear();

                mList.addAll(res.getList());

                pull_load_lot.setHasMore(mList.size() >= 10);

                mLotAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                dismissDialog();
                if(pull_load_lot.isRefresh()) {
                    pull_load_lot.setRefreshing(false);
                }
            }

            @Override
            public void onThrowable(Throwable e) {
                dismissDialog();
                if(pull_load_lot.isRefresh()) {
                    pull_load_lot.setRefreshing(false);
                }
                ToastUtil.showShort(mContext, "网络请求错误!");
            }
        });
    }

    @Override
    public void onRefresh() {
        getHistory();
    }

    @Override
    public void onLoadMore() {
        LotApiIO.getInstance().getLotHistory(lotId, page, new ECallback<LotteryHistory, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(LotteryHistory res) {
                pull_load_lot.loadMoreCompleted();

                if(mList == null) {
                    return;
                }

                page++;

                mList.addAll(res.getList());

                pull_load_lot.setHasMore(res.getList().size() >= 10);

                mLotAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                pull_load_lot.loadMoreCompleted();
            }

            @Override
            public void onThrowable(Throwable e) {
                pull_load_lot.loadMoreCompleted();
                ToastUtil.showShort(mContext, "网络请求错误!");
            }
        });
    }
}
